﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hi_Baidu.Packet.InPacket
{
    /// <summary>
    ///     添加用户分组回馈包
    /// </summary>
    class AddItemReplyPacket : BaseInPackage
    {
        public AddItemReplyPacket(InPackage inpackage) : base(inpackage)
        {
            
        }

        public override void InitPackage()
        {
            int Offset = 0;
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            byte[] ItemKeyLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemKeyLength, 0, 4);
            Offset += 4;
            byte[] ItemKey = new byte[BitConverter.ToInt32(ItemKeyLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemKey, 0, ItemKey.Length);
            Offset += ItemKey.Length;
            byte[] SessionLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, SessionLength, 0, 4);
            Offset += 4;
            byte[] Session = new byte[BitConverter.ToInt32(SessionLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Session, 0, Session.Length);
            Offset += Session.Length;
            byte[] ItemId = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemId, 0, 4);
            Offset += 4;
            byte[] UpdateState = new byte[1];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UpdateState, 0, 1);
            Offset += 1;

            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("ITEMKEY", ItemKey);
            this.DIC_CONTENT.Add("SESSION", Session);
            this.DIC_CONTENT.Add("ITEMID", ItemId);
            this.DIC_CONTENT.Add("UPDATESTATE", UpdateState);
        }

        /// <summary>
        ///     得到当前分组KEY
        /// </summary>
        /// <returns>
        ///     返回分组KEY
        /// </returns>
        public String GetItemKey()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["ITEMKEY"]);
        }

        /// <summary>
        ///     得到当前分组ID
        /// </summary>
        /// <returns>
        ///     返回当前分组ID
        /// </returns>
        public String GetItemId()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["ITEMID"], 0).ToString();
        }

        /// <summary>
        ///     得到当前更新的状态
        /// </summary>
        /// <returns>
        ///     返回更新的状态
        /// </returns>
        public bool GetUpdateState()
        {
            return BitConverter.ToBoolean(this.DIC_CONTENT["UPDATESTATE"], 0);
        }
    }
}
